<?php
namespace App\Admin\Controllers\Basics;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysICountry;
use App\Models\Basics\SysIArea;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class SysIAreaController extends AdminController{

    protected $title = '库存站点';

    protected function grid()
    {
        $grid = new Grid(new SysIArea());
        $grid->column('id','ID')->style('width: 5%');
        $grid->column('area.area','编码');
        $grid->column('area.area_cn','名称');
        $grid->column('country','国家')->pluck('country')->label();
        $grid->column('muser_id','负责人')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });

        $grid->disableRowSelector();
        $grid->disableExport();

        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->equal('country.id', '国家')->select(SysICountry::options());
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('area_id','站点')->select(SysIArea::options());
            });
        });

        return $grid;
    }

    public function form()
    {
        $form = new Form(new SysIArea());

        $form->hidden('id');

        $form->select('area_id', '站点')
            ->options(SysArea::options())
            ->creationRules(['required', "unique:b_sys_i_area"])
            ->updateRules(['required', "unique:b_sys_i_area,area_id,{{id}}"])->setWidth(3);
        $form->select('muser_id','负责人')->options(User::getAMUserOptions(FALSE))->setWidth(3)->rules('required');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });
        return $form;
    }
}
